//
//  SidePersonMenuVC.m
//  Cunli
//
//  Created by ZhanyaaLi on 15/12/3.
//  Copyright © 2015年 lilubin. All rights reserved.
//

#import "SidePersonMenuVC.h"
#import "UIImageView+WebCache.h"
#import "Config.h"
#import "UIViewController+MMDrawerController.h"
#import "CunLiShiManagerViewController.h"
#import "CunWuListViewController.h"
#import "VillageIntroManageVC.h"
#import "ShareAppVC.h"
#import "IncomeDetailVC.h"
#import "MyStudyAndCollectionViewController.h"
#import "PersonalSettingVC.h"
#import "PersonHeaderView.h"
#import "UIColor+expanded.h"
#import "SideMenuCell.h"
#import "MyHelpAndTopicViewController.h"
#import "MyDisputeAndHelpViewController.h"
#import "SwipableViewController.h"
#import "VillageRuleManagerViewController.h"
#import "ChangePersonalInfoVC.h"
#import "weatherBtn.h"
#import "WeatherInfoVC.h"

#define KCLPORTRAITCHANGE @"portraitChanged"
#define KCLTRUENAMECHANGE @"truenameChanged"
#define kTableViewCell @"SideMenuCell"
#define SideCellFont 15

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface SidePersonMenuVC () <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_roleArray;
}
@end

@implementation SidePersonMenuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"0x1f1f1f"]; //1f1f1f
    [self initGlobalParams];
    [self createUserHeaderView];
    [self createTabelView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initGlobalParams
{
    _roleArray = [NSArray arrayWithObjects:@"村民", @"律师", @"", @"调解员", @"联络员", nil];
}

- (void)createUserHeaderView
{
//    PersonHeaderView *headView = [[PersonHeaderView alloc] initWithFrame:CGRectMake(0, 22, LEFTSIDEWIDTH, 60)];
    
    PersonHeaderView *headView = [[[NSBundle mainBundle] loadNibNamed:@"PersonHeaderView" owner:self options:nil] lastObject];
    [headView setFrame:CGRectMake(0, 30, LEFTSIDEWIDTH, 60)];
    [self.view addSubview:headView];
#pragma mark - 添加“设置按钮”
    UIImageView *settingImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, kScreenHeight-33, 18, 18)];
    [settingImage setImage:[UIImage imageNamed:@"sideMenu_Setting"]];
    [self.view addSubview:settingImage];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jumpToSettingVC)];
    settingImage.userInteractionEnabled = YES;
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [settingImage addGestureRecognizer:tap];
    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingBtn setFrame:CGRectMake(38, kScreenHeight-33, 50, 18)];
    [settingBtn.titleLabel setFont:[UIFont systemFontOfSize:SideCellFont]];
    [settingBtn setTitle:@"设置" forState:UIControlStateNormal];
    settingBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [settingBtn setTintColor:[UIColor whiteColor]];
    [settingBtn addTarget:self action:@selector(jumpToSettingVC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:settingBtn];
    
    weatherBtn *weatherButton = [[weatherBtn alloc] initWithFrame:CGRectMake(88, kScreenHeight-42, LEFTSIDEWIDTH - 88, 38)];
    [self.view addSubview:weatherButton];
    weatherButton.buttonClicked = ^(NSDictionary *weatherDic){
        [self jumpToWeatherVC:weatherDic];
    };
    
    headView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapHeader = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jumpToPersonalSettingVC)];
    tapHeader.numberOfTapsRequired = 1;
    tapHeader.numberOfTouchesRequired = 1;
    [headView addGestureRecognizer:tapHeader];
    
}



- (void)createTabelView
{
    /*
    UIView *separateLine = [[UIView alloc] initWithFrame:CGRectMake(0, 109, LEFTSIDEWIDTH, 1)];
    [separateLine setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:separateLine];
    */
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 110, LEFTSIDEWIDTH, kScreenHeight - 110 - 49) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor colorWithHexString:@"0x1f1f1f"];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.sectionHeaderHeight = 0;
    tableView.sectionFooterHeight = 0;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UINib *nib = [UINib nibWithNibName:@"SideMenuCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:kTableViewCell];
    [self.view addSubview:tableView];
}

- (void)setContentViewController:(UIViewController *)viewController
{
    viewController.hidesBottomBarWhenPushed = YES;
    UINavigationController *nav = (UINavigationController *)((UITabBarController *)self.mm_drawerController.centerViewController).selectedViewController;
    [nav pushViewController:viewController animated:YES];
    [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
    }];
}

- (void)jumpToSettingVC
{
    PersonalSettingVC *settingVC = [[PersonalSettingVC alloc] init];
    [self setContentViewController:settingVC];
}

- (void)jumpToWeatherVC:(NSDictionary *)dic
{
    WeatherInfoVC *weatherVC = [[WeatherInfoVC alloc] init];
    weatherVC.weatherDic = dic;
    [self setContentViewController:weatherVC];
}



- (void)jumpToPersonalSettingVC
{
    ChangePersonalInfoVC *personalInfo = [[ChangePersonalInfoVC alloc] init];
    [self setContentViewController:personalInfo];
}

#pragma mark - UITableView delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            if ([[Config getGroupID] isEqualToString:@"9"]) {
                return 5;
            }
            else
            {
                return 0;
            }
            break;
        }
        case 1:
            return 2;
            break;
        case 2:
            return 3;
            break;
        case 3:
            return 1;
            break;
        default:
            break;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    return cell;
    NSArray *section0Array = [NSArray arrayWithObjects:@"村里事管理", @"通知通告管理", @"村务公开管理", @"本村介绍", @"村规民约", nil];
    NSArray *section1Array = [NSArray arrayWithObjects:@"村里分享", @"积分商城",  nil];
    NSArray *section2Array = [NSArray arrayWithObjects:@"我的乡情", @"我的学习", @"我的纠纷", nil];
    
    SideMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCell];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MineTableViewCell" owner:nil options:nil] lastObject];
    }
    switch (indexPath.section) {
        case 0:
        {
            cell.title = section0Array[indexPath.row];
        }
            break;
        case 1:
        {
            cell.title = section1Array[indexPath.row];
            break;
        }
        case 2:
        {
            cell.title = section2Array[indexPath.row];
            break;
        }
        case 3:
        {
            cell.title = @"我的收藏";
            break;
        }
        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    UIView *selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LEFTSIDEWIDTH, 100)];
    selectedView.backgroundColor = [UIColor blackColor];
    cell.selectedBackgroundView = selectedView;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0: //村里事管理
                {
                    CunLiShiManagerViewController *cunLiVC1=[[CunLiShiManagerViewController alloc]init];
                    cunLiVC1.type=1;
                    [self setContentViewController:cunLiVC1];
                    break;
                }
                case 1: //通知通告管理
                {
                    CunLiShiManagerViewController *cunLiVC2=[[CunLiShiManagerViewController alloc]init];
                    cunLiVC2.type=2;
                    [self setContentViewController:cunLiVC2];
                    break;
                }
                case 2: //村务公开
                {
                    CunWuListViewController *cunWuVC=[[CunWuListViewController alloc]init];
                    cunWuVC.isManager=YES;
                    [self setContentViewController:cunWuVC];
                    break;
                }
                case 3: //本村介绍
                {
                    VillageIntroManageVC *villageIntroVC = [[VillageIntroManageVC alloc] init];
                    [self setContentViewController:villageIntroVC];
                    break;
                }
                case 4: //村规民约
                {
                    VillageRuleManagerViewController *ruleVC = [[VillageRuleManagerViewController alloc] init];
                    [self setContentViewController:ruleVC];
                    break;
                }
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0: //分享村里
                {
                    ShareAppVC *shareVC = [[ShareAppVC alloc] init];
                    [self setContentViewController:shareVC];
                    break;
                }
                case 1: //积分详情
                {
                    IncomeDetailVC *pointVC = [[IncomeDetailVC alloc] init];
                    [self setContentViewController:pointVC];
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case 2:
        {
            switch (indexPath.row) {
                case 0:
                {
                    NSLog(@"我的乡情");
                    MyHelpAndTopicViewController *helpVC=[[MyHelpAndTopicViewController alloc]init];
//                    helpVC.hidesBottomBarWhenPushed=YES;
//                    helpVC.navTitle=@"我的乡情";
                    helpVC.type=1;
//                    [self.navigationController pushViewController:helpVC animated:YES];
                    [self setContentViewController:helpVC];

                    break;
                }
                case 1:
                {
                    MyStudyAndCollectionViewController *studyVC=[[MyStudyAndCollectionViewController alloc]init];
                    studyVC.navTitle=@"我的学习";
                    studyVC.type=2;
                    [self setContentViewController:studyVC];
                    break;
                }
                case 2:
                {
                    NSLog(@"我的纠纷");
                    MyDisputeAndHelpViewController *disputeVC = [[MyDisputeAndHelpViewController alloc] initWithmyType:1];
                    MyDisputeAndHelpViewController *helpVC = [[MyDisputeAndHelpViewController alloc] initWithmyType:2];
                    NSMutableArray *myVCS = [NSMutableArray arrayWithObjects:disputeVC, helpVC,nil];
                    SwipableViewController *childSwipableVC = [[SwipableViewController alloc] initWithTitle:@"我的纠纷" andSubTitles:@[@"纠纷调解", @"法律援助"] andControllers:myVCS underTabbar:NO];
//                    childSwipableVC.hidesBottomBarWhenPushed = YES;
//                    [self.navigationController pushViewController:childSwipableVC animated:YES];
                    [self setContentViewController:childSwipableVC];
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case 3:
        {
            MyStudyAndCollectionViewController *studyVC=[[MyStudyAndCollectionViewController alloc]init];
            studyVC.navTitle=@"我的收藏";
            studyVC.type=1;
            [self setContentViewController:studyVC];
            break;
        }
        default:
            break;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}



@end
